package com.hnx.admin.security.handler;


import com.hnx.admin.api.http.HttpStatusAPI;
import com.hnx.admin.utils.ResponseUtil;
import com.hnx.admin.web.json.ResultJson;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/*
创建人： 倔强的头发
创建时间：2025/5/16
功能描述:

*/
@Slf4j
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, HttpStatusAPI {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        String msg = String.format("请求访问：%s，认证失败，无法访问系统资源", request.getRequestURI());
        log.error(msg);
        ResponseUtil.write(response, com.alibaba.fastjson2.JSON.toJSONString(ResultJson.fail(UNAUTHORIZED, msg)));
    }
}
